package com.sukmoonlee.dns.jWhois;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		if(args.length==0) {
			PrintHelp(false);
			return;
		} 
			
		WhoisClient client=new WhoisClient();
		int count=0;
		int status=0;
		
		for(int i=0;i<args.length;i++) {
			if( "--".equals(args[i].substring(0, 2)) ) {
				
				if("--help".equals(args[i])) {
					PrintHelp(false);
					return;
				} else if("--version".equals(args[i])) {
					PrintHelp(true);
					return;
				//} else if("--verbose".equals(args[i])) {
				//	client.setFlagVerbose(true);
				} else if("--no-redirect ".equals(args[i])) {
					client.setFlagRedirect(false);
				//} else if("--no-whoisservers".equals(args[i])) {
				//	client.setFlagWhoisServers(false);
				} else if("--display-redirections".equals(args[i])) {
					client.setFlagDisplayRedirect(true);
				//} else if("--raw".equals(args[i])) {
				//	client.setFlagRaw(true);
				} else if(args[i].length()>=8 && "--host=".equals(args[i].substring(0,7))) {
					client.setServerName(args[i].substring(8));
				} else {
					System.out.println("Invalid option: "+args[i]);
					PrintHelp(false);
					status=9199; break;
				}
			} else if( "-".equals(args[i].substring(0, 1)) ) {
				if("-p".equals(args[i])) {
					if(i+1==args.length) { status=9101; break; }
					i++;					
					client.setServerPort(Integer.parseInt(args[i]));
				} else if("-h".equals(args[i])) {
					if(i+1==args.length) { status=9102; break; }
					i++;					
					client.setServerName(args[i]);
				//} else if("-v".equals(args[i])) {
				//	client.setFlagVerbose(true);
				} else if("-n".equals(args[i])) {
					client.setFlagRedirect(false);
				//} else if("-s".equals(args[i])) {
				//	client.setFlagWhoisServers(false);
				} else if("-i".equals(args[i])) {
					client.setFlagDisplayRedirect(true);
				//} else if("-a".equals(args[i])) {
				//	client.setFlagRaw(true);
				} else {
					System.out.println("Invalid option: "+args[i]);
					PrintHelp(false);
					status=9299; break;
				}
			} else {
				if(count==0) {
					client.setQueryDomain(args[i]);
				} else {
					System.out.println("Invalid option: "+args[i]);
					PrintHelp(false);
					status=9399; break;
				}
				count++;
			}
		}

		if(client.getName()==null || "".equals(client.getName())) {
			PrintHelp(false);
			status=9000;
		}
		
		if(status!=0) {
			if(status%100!=99) {
				System.out.println("Command Line Error Code: "+status);
			}
			return;
		}
		
		client.start();
	}

	static private void PrintHelp(boolean b) {
		
		//jwhois version 3.2.2, Copyright (C) 1999-2003  Free Software Foundation, Inc.
		//This program is free software with ABSOLUTELY NO WARRANTY; you may
		//redistribute it under the terms of the GNU General Public License.
		System.out.println(Constant.PG_NAME+" version "+Constant.PG_VERSION+"-"+Constant.PG_RELEASE+", Copyright (C) 2012 SukmoonLee.");
		System.out.println("");
		
		if(b==true) { return; }
		
		System.out.println("Usage: java -jar jWhois.jar  [OPTIONS] [QUERY]");
		System.out.println("	  --version                  display version number and patch level");
		System.out.println("	  --help                     display this help");
		//System.out.println("	  -v, --verbose              verbose debug output");
		//System.out.println("	  -c FILE, --config=FILE     use FILE as configuration file");
		System.out.println("	  -h HOST, --host=HOST       explicitly query HOST");
		System.out.println("	  -n, --no-redirect          disable content redirection");
		//System.out.println("	  -s, --no-whoisservers      disable whois-servers.net service support");
		//System.out.println("	  -a, --raw                  disable reformatting of the query");
		System.out.println("	  -i, --display-redirections display all redirects instead of hiding them");
		System.out.println("	  -p PORT                    use port number PORT (in conjunction with HOST)");
		//System.out.println("	  -r, --rwhois               force an rwhois query to be made");
		//System.out.println("	  --rwhois-display=DISPLAY   sets the display option in rwhois queries");
		//System.out.println("	  --rwhois-limit=LIMIT       sets the maximum number of matches to return");
        System.out.println("");
        System.out.println("");
		System.out.println("	Report bugs to smlee@sukmoonlee.com");

	}
	
}
